package identical_scheduling_algorithm;

import de.cau.apo.problems.identicalscheduling.Job;

/**
 * Eine Implementierung des ListScheduling Algorithmus. 
 * 
 * Jobs werden hierbei als Array vom Typ Job dargestellt.
 * Siehe hierzu auch @see de.cau.apo.problems.identicalscheduling.Job.
 * 
 * @author Oliver Hense
 * @author Tim Dopke
 *
 */

public class ListScheduling {
	
	int numberOfJobs;
	int numberOfMachines;
	Job [] jobs;
	
	
	public ListScheduling(int numberOfMachines, Job[] jobs){
		this.jobs = jobs;
		this.numberOfMachines = numberOfMachines;
		this.numberOfJobs = jobs.length;
	}
	
	public double solve(){
		
		int a [] = new int[numberOfMachines];
		for (int i = 0; i< numberOfJobs; i++){
			int pos = findMin(a);
			a[pos] = a[pos] + jobs[i].size;
		}
		double max = 0.;
		for (int i = 0; i< numberOfMachines;i++)
			max = Math.max(max,a[i]);
		return max;
	}
	
	public int findMin( int [] a){
		int	min = Integer.MAX_VALUE;
		int pos = 0;
		
		for(int i = 0; i< a.length;i++){
			if(min > a[i]){
				min = a[i];
				pos = i;	
			}
				
		}
			 
		
		return pos;
	}

}
